package com.example.hetong_system.config;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

// 读取application.yml中前缀为alipay的配置
@Data
@Component
@ConfigurationProperties(prefix = "alipay")
public class AlipayConfig {
    private String appId;
    private String appPrivateKey;
    private String alipayPublicKey;
    private String notifyUrl;
//    private String ReturnUrl;  // 确保该字段存在

    @PostConstruct
    public void init() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi-sandbox.dl.alipaydev.com"; // 沙箱网关
        config.signType = "RSA2";
        // 注入配置参数（包括returnUrl）
        config.appId = this.appId;
        config.merchantPrivateKey = this.appPrivateKey;
        config.alipayPublicKey = this.alipayPublicKey;
        config.notifyUrl = this.notifyUrl;
//        config.ReturnUrl = this.ReturnUrl;  // 2.2.2版本支持该字段

        Factory.setOptions(config);
//        System.out.println("支付宝SDK初始化成功，returnUrl=" + this.returnUrl);

        // 打印关键配置（调试用）
        System.out.println("==== 支付宝SDK初始化 ====");
        System.out.println("AppID: " + appId);
        System.out.println("私钥长度: " + appPrivateKey.length() + " 字符");
        System.out.println("网关: " + config.gatewayHost);
        System.out.println("签名类型: " + config.signType);
        System.out.println("=======================");
    }
}